import {
	URLs,
	imgPrefix
} from '@/config/index.js'

/**
 * 格式化时间
 * @param time 秒、毫秒
 */
function formatDateTime(time) {
	var oDate = new Date(time),
		oYear = oDate.getFullYear(),
		oMonth = oDate.getMonth() + 1,
		oDay = oDate.getDate(),
		oHour = oDate.getHours(),
		oMin = oDate.getMinutes(),
		oSen = oDate.getSeconds(),
		oTime = oYear + '-' + addZero(oMonth) + '-' + addZero(oDay) + ' ' + addZero(oHour) + ':' + addZero(oMin) + ':' +
		addZero(oSen);
	return oTime;

	//补零操作
	function addZero(num) {
		if (parseInt(num) < 10) {
			num = '0' + num;
		}
		return num;
	}
}

/**
 * 格式化数字，保留两位小数
 * 解决数字精度丢失问题
 */
function formatNumber(num) {
	return Math.round(num * Math.pow(10, 2)) / Math.pow(10, 2);
}

/**
 * 格式化资源文件
 */
function formatFile(fileName) {
	if (!fileName) return '';
	if (fileName.indexOf('http') == -1) {
		return String(imgPrefix + fileName)
	} else {
		return fileName
	}
}

// 格式化金额
function formatMoney(amount) {
	amount = Number(amount);
	let formattedAmount = 0
	
	if (amount >= 10000) {
		formattedAmount = formatNumber((amount / 10000)) + '万';
	} 
	else if (amount >= 1000) {
		formattedAmount = formatNumber((amount / 1000)) + '千';
	} 
	else {
		formattedAmount = '￥' + formatNumber(amount);
	}
	return formattedAmount;

}

export default {
	formatDateTime,
	formatNumber,
	formatFile,
	formatMoney
}